﻿using GameCore;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using System;
using Utils;


namespace RoundTicks.Event
{
    public class RoundTickEvent
    {
        [PluginEvent(ServerEventType.WaitingForPlayers)]
        public void OnWaitingLobby(WaitingForPlayersEvent ev)
        {
            RoundTickLoad.Singleton.IsRoundWaiting = true;
            Round.IsLobbyLocked = true;

            MessageStringUtils.GenerateAdminMessage($"新对局准备中 [{Player.Count}], 请稍等...", out string waiting);
            Server.SendBroadcast(waiting, 10, Broadcast.BroadcastFlags.Normal, true);

            Action voteAct = () =>
            {
                string message = $"输入 [.T 编号] || 投票开局SCP阵容 \n\n{RoundTickLoad.Singleton.TeamRoles.ToString()}";
                MessageStringUtils.GenerateAdminMessage(message, out string voting);
                Server.SendBroadcast(message, 1, Broadcast.BroadcastFlags.Normal, true);
            };

            TimeCoroutine.Delay(10, () => TimeCoroutine.Loop(30, 1, voteAct, () => RoundStart.singleton.Timer = 10));
        }

        [PluginEvent(ServerEventType.PlayerGameConsoleCommandExecuted)]
        public void OnCommandExecuted(PlayerGameConsoleCommandExecutedEvent ev)
        {
            if (Round.IsRoundStarted) return;

            //Log.Info($"命令行|{ev.Command}|来自 {ev.Player.Nickname}|{ev.Command.ToUpper().StartsWith("T")}");

            if (!ev.Command.ToUpper().StartsWith("T")) return;

            PluginAPI.Core.Log.Info($"命令行|解析内容：{ev.Arguments[0]}|来自 {ev.Player.Nickname}|{ev.Player.PlayerId}");

            RoundTickLoad.Singleton.TeamRoles.TickRole(ev.Arguments[0], ev.Player.PlayerId.ToString());
        }



        public void OnRoundStart(RoundStartEvent ev)
        {
            if (!RoundTickLoad.Singleton.Config.IsEnabled) return;

            RoundTickLoad.Singleton.IsRoundWaiting = false;
            Round.IsLocked = true;

            RoundTickLoad.Singleton.TeamRoles.GetRole();
        }
    }
}
